class Compiler {
    constructor(vm) {
        this.el = vm.$el
        this.vm = vm
        this.compile(this.el)
    }
    //编译主函数
    compile(el) {
        Array.from(el.childNodes).forEach(node => {
            //是否是文本节点
            if(this.isTextNode(node)) {
                this.compileText(node)
            } else if(this.isElementNode(node)) {
                this.compileElement(node)
            }
            //模版下的子节点不止一层，这里递归调用compile
            if(node.childNodes && node.childNodes.length) this.compile(node)
        })
    }
    //编译元素节点内容
    compileElement(node) {
        //遍历所有的属性节点
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name
            
            if(this.isDirective(attrName)) {
                //v-text --> text
                attrName = attrName.substr(2)
                let key = attr.value //属性的值
                this.update(node, key, attrName)
            }
        })
    }
    update(node, key, attrName) {
        //额外处理v-on
        if(attrName.indexOf('on:') !== -1) {
            const event = attrName.split(':')[1]//事件名
            this.onUpdater && this.onUpdater(node, event, key)
        } else {
            let updateFn =  this[attrName + 'Updater']
            updateFn && updateFn.call(this, node, this.vm[key], key) //这里call很重要，因为updateFn方法里的this并不指向vue，通过call让this绑定到vue
        }
    }

    //处理v-text指令
    textUpdater(node, value, key) {
        node.textContent = value
        //创建观察者watcher
        new Watcher(this.vm, key, (newValue) => {
            node.textContent = newValue
        })
    }
    //处理v-on指令
    onUpdater(node, event, method) {
        //添加事件
        node.addEventListener(event, () => {
            //调用vue中挂载的方法
            const fn = this.vm[method]
            fn()
        })
    }
    //处理v-html指令
    htmlUpdater(node, value, key) {
        node.innerHTML = value
        //创建观察者
        new Watcher(this.vm, key, (newValue) => {
            node.innerHTML = newValue
        })
    }
    //处理v-model指令
    modelUpdater(node, value, key) {
        node.value = value
        //创建观察者watcher
        new Watcher(this.vm, key, (newValue) => {
            node.value = newValue
        })
        node.addEventListener('input', () => {
            this.vm[key] = node.value
        })
    }

    //编译文本节点内容
    compileText(node) {
        //{{  msg }}
        const reg = /\{\{(.+?)\}\}/
        let value = node.textContent
        if(reg.test(value)) {
            let key = RegExp.$1.trim() //正则匹配到的第一个结果并去除头尾空格
            node.textContent = this.vm[key]
            //创建观察者watcher
            new Watcher(this.vm, key, (newValue) => {
                node.textContent = newValue
            })
        }
    }
    //判断节点的attr属性是否是指令 'v-'开头
    isDirective(attrName) {
        return attrName.startsWith('v-')
        
    }
    //判断是否是文本节点
    isTextNode(node) {
        return node.nodeType === 3
    }
    //判断是否是元素节点
    isElementNode(node) {
        return node.nodeType === 1
    }
    
}